//
//  MainScene.cpp
//  SimpleGame
//
//  Created by Scar1et on 2017/7/24.
//
//

// 头文件 -- 类库声明请放入头文件
#include "MainScene.h"

// 层级：影响显示与事件响应
enum {
    // 背景
    levelBGSimple = 1,
    // 内容
    levelContentSimple = 101,
    // 对话框
    levelDialogSimple = 1001
};

// TAG：视图标记
enum {
    tagWidget = 1,
    tagScheduler = 2,
    tagTouch = 3
};

// 创建场景
Scene* MainScene::createScene() {
    Scene* sMain = Scene::create();
    sMain->addChild(MainScene::create());
    return sMain;
}

// 初始化场景，自动调用
bool MainScene::init() {
    // 先调用父类Init
    if (!Layer::init()) {
        return false;
    }
    
    // 手机按键监听器
    listenerKeyboard = EventListenerKeyboard::create();
    listenerKeyboard->onKeyReleased = CC_CALLBACK_2(MainScene::onKeyReleased, this);
    // 添加到事件分发器中
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerKeyboard, this);
    
    // 开始初始化
    // 成员变量初始化
    // 可视区域尺寸
    sizeVisible = Director::getInstance()->getVisibleSize();
    // 可视区域的出发点坐标
    positionOrigin = Director::getInstance()->getVisibleOrigin();
    // 屏幕尺寸
    sizeWin = Director::getInstance()->getWinSize();
    
    this->initContent();
    
    // 初始化成功
    return true;
}

// 内容初始化
void MainScene::initContent() {
    // 控件按钮
    auto lWidget = Label::createWithSystemFont("控件", "arial.ttf", 28);
    lWidget->setTextColor(Color4B::WHITE);
    lWidget->setHorizontalAlignment(TextHAlignment::LEFT);
    lWidget->setVerticalAlignment(TextVAlignment::CENTER);
    lWidget->setDimensions(120, 40);
    auto milWidget = MenuItemLabel::create(lWidget, CC_CALLBACK_1(MainScene::onClicked, this));
    milWidget->setTag(tagWidget);
    milWidget->setPosition(Vec2(positionOrigin.x + 80, positionOrigin.y + sizeVisible.height - 100));
    
    // 调度器按钮
    auto lScheduler = Label::createWithSystemFont("定时器", "arial.ttf", 28);
    lScheduler->setTextColor(Color4B::WHITE);
    lScheduler->setHorizontalAlignment(TextHAlignment::LEFT);
    lScheduler->setVerticalAlignment(TextVAlignment::CENTER);
    lScheduler->setDimensions(120, 40);
    auto milScheduler = MenuItemLabel::create(lScheduler, CC_CALLBACK_1(MainScene::onClicked, this));
    milScheduler->setTag(tagScheduler);
    milScheduler->setPosition(Vec2(positionOrigin.x + 220, positionOrigin.y + sizeVisible.height - 100));
    
    // 触控按钮
    auto lTouch = Label::createWithSystemFont("触控", "arial.ttf", 28);
    lTouch->setTextColor(Color4B::WHITE);
    lTouch->setHorizontalAlignment(TextHAlignment::LEFT);
    lTouch->setVerticalAlignment(TextVAlignment::CENTER);
    lTouch->setDimensions(120, 40);
    auto milTouch = MenuItemLabel::create(lTouch, CC_CALLBACK_1(MainScene::onClicked, this));
    milTouch->setTag(tagTouch);
    milTouch->setPosition(Vec2(positionOrigin.x + 360, positionOrigin.y + sizeVisible.height - 100));
    
    // 按钮菜单 -- main【可添加多个，用NULL结尾】
    auto mMain = Menu::create(milWidget, milScheduler, milTouch, NULL);
    mMain->setPosition(Vec2::ZERO);
    this->addChild(mMain, levelContentSimple);
}

// 点击监听
void MainScene::onClicked(Ref* rSender) {
    int tag = ((MenuItem*)rSender)->getTag();
    switch (tag) {
        case tagWidget:
            // 控件按钮被点击
            // 将运行中的场景暂停，并push到场景堆栈中，运行新的场景
            Director::getInstance()->pushScene(WidgetScene::createScene());
            break;
        case tagScheduler:
            // 调度器按钮被点击
            // 将运行中的场景暂停，并push到场景堆栈中，运行新的场景
            Director::getInstance()->pushScene(SchedulerScene::createScene());
            break;
        case tagTouch:
            // 触控按钮被点击
            // 将运行中的场景暂停，并push到场景堆栈中，运行新的场景
            Director::getInstance()->pushScene(TouchScene::createScene());
            break;
        default:
            break;
    }
}

// 键盘点击监听
void MainScene::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event) {
    switch (keyCode){
        case EventKeyboard::KeyCode::KEY_BACK:
            // 返回键
            // 从事件分发器移除手机按键监听器
            Director::getInstance()->getEventDispatcher()->removeEventListener(listenerKeyboard);
            // 关闭应用
            Director::getInstance()->end();
            break;
        default:
            break;
    }
}
